<?php
namespace app\Backend\controller;;
use think\Db;
use clt\Leftnav;
use app\Backend\model\Activity; //活動
use app\Backend\model\Notice;   //公告-推送通知
use app\Backend\model\Email;    //郵箱
use think\Validate;
class System extends Common
{
    public function _initialize(){
        parent::_initialize();
    }
    public function index()
    {

        return $this->fetch();
    }
    //=========================================== 活动管理 ======================================================
    //活动列表
    public function activityList(){

        $list = db('activity')->order('id desc')->select();
        $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        $this->assign('lists',$result['data']);
        return view();
    }

    //更改活動狀態
    public function actStatus(){
        $id=input('id');
        $status = input('status');
        if(db('activity')->where(array('id'=>$id))->setField(['status'=>$status])){
            return ['code'=>1,'msg'=>''];
        }
        return ['code'=>0,'msg'=>'操作失败'];

    }

    //删除活動
    public function actDel(){
        $id             = input('id');
        $map['id']      = $id;
        if (db('activity')->where($map)->delete()){
            return $result = ['code'=>1,'msg'=>'删除成功!','url'=>'/reload'];
        }else{
            return $result = ['code'=>0,'msg'=>'活動删除失败!','url'=>''];
        }
    }

    //修改活动
    public function actEdit(){
        if(request()->isPost()){
            $data   = input('post.');
            //验证活动名是否唯一
            $map['id']    = array('neq',input('post.id'));
            $where['id']  = array('eq',input('post.id'));
            if(input('post.activityname')){
                $map['activityname'] = input('post.activityname');
                $check_user = Activity::get($map);
                if ($check_user) {
                    return $result = ['code'=>0,'msg'=>'活动已存在，请重新输入活动名!'];
                }
            }
            //2.验证字段
            $msg = $this->validate($data,'Activity');
            if($msg!='true'){
                return $result = ['code'=>0,'msg'=>$msg];
            }
            //3.验证图片文件是否更改
            $res = Activity::get(input('post.id'));
            if (input('post.img') != $res->img) {
                unlink('.'.$res->img); //是则删除
            }
            Activity::update($data);
            return $result = ['code'=>1,'msg'=>'活动修改成功!','url'=>'/reload'];
        }else{
            $info = Activity::get(['id'=>input('id')]);
            $this->assign('row', $info);
            // $this->assign('title',lang('edit').lang('admin'));
            return view('activityForm');
        }
    }

    //添加活動
    public function actAdd(){
        if(request()->isPost()){
            $data = input('post.');

            //1.验证是否唯一
            $check_user = Activity::get(['activityname'=>$data['activityname']]);
            if ($check_user) {
                return $result = ['code'=>0,'msg'=>'活动名已存在，请重新输入活动名!'];
            }
            $data['add_time'] = time();
            $data['ip'] = request()->ip();
            $data['status'] = 1;

            //2.验证
            $msg = $this->validate($data,'app\Backend\validate\Activity');
            if($msg!='true'){
                return $result = ['code'=>0,'msg'=>$msg];
            }

            //3.添加成功
            if (Activity::create($data)) {
                return ['code'=>1,'msg'=>'活動添加成功!','url'=>'/reload'];
            } else {
                return ['code'=>0,'msg'=>'活動添加失败!'];
            }
        }else{
            //展示添加页面
            $this->assign('title',lang('add').lang('admin'));
            $this->assign('info','null');
            $this->assign('selected', 'null');
            return view('activityForm');
        }
    }

    //=========================================== 公告管理 ======================================================

    //公告列表
    public function noticeList(){

        $list = db('user')->order('id desc')->select();
        $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        $this->assign('lists',$result['data']);
        return view();
    }

    //=========================================== 邮箱管理 ======================================================


    //邮箱配置列表
    public function emailList(){

        $list = db('user')->order('id desc')->select();
        $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        $this->assign('lists',$result['data']);
        return view();
    }

    //修改订单
    public function orderEdit(){
        $id             = input('id');
        $map['id']      = $id;
        $map['is_sys']  = 0;
        return $result = ['code'=>0,'msg'=>'订单修改失败!','url'=>''];
    }

    //取消订单--状态改为取消
    public function orderEditStatus(){
        $id             = input('id');
        $map['id']      = $id;
        $map['is_sys']  = 0;
        return $result = ['code'=>0,'msg'=>'订单取消失败!','url'=>''];
    }

    //删除订单
    public function orderDel(){
        $id             = input('id');
        $map['id']      = $id;
        if (db('order')->where($map)->delete()){
            return $result = ['code'=>1,'msg'=>'删除成功!','url'=>'/reload'];
        }else{
            return $result = ['code'=>0,'msg'=>'用户信息删除失败!','url'=>''];
        }
    }


}